/**
 * Created by guzhe on 2016/4/28.
 */

angular.module('myApp')

    .controller('queryCtrl', ['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'limitToFilter', 'filterFilter', 'myConstant', 'growl', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                                                          CacheService, limitToFilter, filterFilter, myConstant, growl) {
            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            //变量初始化
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            //查询条件
            vm.queryBean = {};

            vm.validateOptions = {
                blurTrig: true,
                showError: function (elem, errorMessages) {
                    growl.addErrorMessage(errorMessages);
                    // angular.element(elem).after('<span>' + errorMessages + '</span>');
                },
                removeError: true
            };

            //缓存数据初始化(需要缓存的key请自定义)
            /*数据格式{
             key1 :value1,
             key2:value2
             }*/
            vm.cached = {
                MERCHANT_PROPERTY: {},
                CUSTPROPERTY: {},
                RISK_EVENT_CHANNEL: {},
                INFORMATION_LEVEL: {},
                PERSONAL_RISK_TYPE: {},
                QUERY_RANGE: {},
                VALIDITY :{}
            };
            //批量获取缓存
            var keys = [];
            for (var key in  vm.cached) {
                keys.push(key);
            }
            $log.debug('批量查询缓存');
            $http.post(config.ctx + '/specially/dataDic/cache/get', keys, config.jsonHeader)
                .then(function (response) {
                    var tempMap = response.data.object;
                    for (var key in vm.cached) {
                        if (!angular.isUndefined(tempMap[key])) {
                            vm.cached[key] = tempMap[key];
                        }
                    }
                }, function (response) {
                    $log.error('获取数据%s失败', keys);
                })

           // CacheService.initCache(vm.cached);


            /*将前面缓存的数据格式化
             [{key:value},{key:value}]*/
            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };

            //获取对象数据[user,user,user]
            vm.getObj = function (key, input) {
                return $http.post(config.ctx + '/base/cache/getAll',
                    $httpParamSerializerJQLike({cacheKey: key}))
                    .then(function (response) {
                        return limitToFilter(filterFilter(response.data.object, input), 6);
                    }, function (response) {
                        $log.error('获取数据%s失败', cacheKey);
                    })
            };

            vm.queryDetail = queryDetail;
            vm.queryDetailInfo = queryDetailInfo;
            vm.resetForm = resetForm;

            /*------------------以上配置通用---------------------*/

          /*  vm.addItem = addItem;
            vm.updateItem = updateItem;
            vm.deleteItem = deleteItem;
            vm.auditItem = auditItem;
            vm.roleAllocate = roleAllocate;
            vm.resetPwd = resetPwd;*/

            /*------------------以上方法名可选择性通用---------------------*/

            /*CacheService.initCache(vm.cached, function (cacheKey, cacheObj) {
             $log.debug(cacheKey, cacheObj);
             vm.cached[cacheKey] = cacheObj;
             });*/

            /*vm.getCache = function (key) {
             CacheService.getCache(key)
             }*/

            function queryDetail() {
                if(isNull(vm.queryBean.scope)){
                    alert("查询范围不能为空")
                    return
                }if(isNull(vm.queryBean.mobileNo)&& isNull(vm.queryBean.mac)&& isNull(vm.queryBean.imei)&& isNull(vm.queryBean.bankNo)&&
                    isNull(vm.queryBean.docCode)&& isNull(vm.queryBean.ip)&& isNull(vm.queryBean.recBankNo)){
                    //alert("手机号、MAC、Imei、银行账/卡号、（客户姓名）身份证证件号码、IP地址、收款银行账/卡号不能全部为空")
                    alert("手机号、银行账/卡号、身份证证件号码、收款银行账/卡号不能全部为空")
                    return
                }
                var queryForm = document.getElementById('queryForm');
                vm.url = angular.element(queryForm).prop('action');

                //组织参数：查询条件 + 分页数据
                vm.postData = vm.queryBean;
                vm.postData.pageSize = vm.pagination.pageSize;
                vm.postData.pageNum = vm.pagination.pageNum;
                $http.post(vm.url, $httpParamSerializerJQLike(vm.postData))
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            vm.pagination = data.object;
                            //清空选中记录
                            CheckboxService.clearChecked();
                            if(isNull(vm.pagination.list)){
                                alert("提示: 查询无记录!");
                            }
                        } else {
                            alert(data.msg);
                        }
                    });

            };
            function isNull(a){
                if (a =="" || a==undefined || a== null) {
                    return true;
                }else{
                    return false;
                }
            };
            function queryDetailInfo(obj) {
                OpenService({
                    modalTitle: '个人风险信息详情',
                    modalBody: 'personRiskDetail',
                    url: 'queryDetail',
                    item: {results: obj},
                    cached: vm.cached
                }, function (iteaddItemm) {
                }, 'QueryDetailModalInstanceCtrl', 'myModalNoSave.html');
            };
            function resetForm() {
                vm.queryBean = {};
            }
        }]);

angular.module('myApp').controller('QueryDetailModalInstanceCtrl',['$scope', '$uibModalInstance', 'modalItem', '$http',
    '$httpParamSerializerJQLike', function ($scope, $uibModalInstance, modalItem, $http,
                                            $httpParamSerializerJQLike) {

        var vm = $scope.vm = {
            modalTitle: 'please change the title',
            modalBody: '#',
            url: '',
            item: {}
        };
        vm = $scope.vm = modalItem;

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };

    }]);


